<template>
    <el-card class="box-card">
        <template #header>
            <div class="card-header">
                <span>注册</span>
            </div>
        </template>
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
            class="demo-ruleForm">

            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirm">
                <el-input v-model="ruleForm.confirm" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">Submit</el-button>
                <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { usernameExist ,reg} from '../services/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';

const ruleFormRef = ref();
const router = useRouter();
const validateUsername = async (rule, value, callback) => {
    let data = await usernameExist(value);
    if (data.status == 1) {
        callback();
    } else {
        callback(new Error('重名'));
    }
}

const validatePass = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        if (ruleForm.confirm !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('confirm', () => null)
        }
        callback()
    }
}
const validatePass2 = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入确认密码'))
    } else if (value !== ruleForm.pwd) {
        callback(new Error("两次密码不一致"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    username: '',
    pwd: '',
    confirm: '',
})

const rules = reactive({
    username: [{ required: true, message: "用户名不能为空" },
    { pattern: /^\w{6,20}$/, message: "用户名必须位6-20位字母或数字组成" }
        , { validator: validateUsername, trigger: 'blur' }],
    pwd: [{ validator: validatePass, trigger: 'blur' }],
    confirm: [{ validator: validatePass2, trigger: 'blur' }],
})

const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
        if (valid) {
            // console.log('submit!')
            await reg(ruleForm.username, ruleForm.pwd);
            ElMessageBox.alert('注册成功', 'Title', {
                // if you want to disable its autofocus
                // autofocus: false,
                confirmButtonText: 'OK',
                callback: (action) => {
                    router.push("/login");
                },
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style>
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.box-card {
    width: 480px;
    margin: auto;
}
</style>